﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PositionManage.aspx.cs" Inherits="Educational_School_PositionManage" %>
<script type="text/javascript">
window.addEvent('domready',function(){
    var position={};
    
    position.deleteForPosition=function(){
        var indices = position.datagrid.getSelectedIndices();
        if (indices.length == 0)
	    {
		    MUI.showModalAlert('您必须至少选定一行');
		    return;
	    }
	    if (indices.length > 1)
	    {
		    MUI.showModalAlert('修改时不允许一次修改多行数据。。');
		    return;
	    }
	    var selectedData=position.datagrid.getDataByRow(indices[0]);
	    MUI.showConfirm("你确认删除这个职位么？请确保此职位没有与之相关教师。此操作不可恢复！",function(){
	        MUI.showModalMessage('正在删除职位');
            new Request.JSON({url:'../Ajax/PositionManage.aspx?Action=deletePosition&positionId='+selectedData.positionId,noCache:true,onSuccess:function(result){
                MUI.hideModalMessage();
                if(result.error)
                    MUI.appendStatusMessage({'text':result.msg});
                else
                {
                    MUI.appendStatusMessage({'text':result.msg});
                    position.datagrid.loadData();
                }
            },onFailure:function(error){
	            MUI.appendStatusMessage({'text':'AJAX提交表单的过程中发生未知错误。'});
	            MUI.hideModalMessage();
            }}).get();
        });
    };
    
    position.updateFormCheck=function(){
        new FormCheck('formUpdateForPosition',{display:{tipsPosition:'left'},submit:false,onValidateSuccess:function(){
            MUI.showModalMessage('正在添加职位');
            new Request.JSON({url:'../Ajax/PositionManage.aspx?Action=saveUpdatePosition',noCache:true,onSuccess:function(result){
                MUI.hideModalMessage();
                if(result.error)
                    MUI.appendStatusMessage({'text':result.msg});
                else
                {
                    $('windowUpdateForPosition').retrieve('instance').close();
                    MUI.appendStatusMessage({'text':result.msg});
                    position.datagrid.loadData();
                }
	        },onFailure:function(error){
		        MUI.appendStatusMessage({'text':'AJAX提交表单的过程中发生未知错误。'});
		        MUI.hideModalMessage();
            }}).post($('formUpdateForPosition'));
        }});   
    };
    
    position.addOptions=function(select,data,departmentParentId,level)
    {
        var j,temp;
        for(var i=0;i<data.length;i++)
        {
            temp='';
            for(var j=0;j<level;j++)
                temp+='－';
            if(data[i].departmentParentId==departmentParentId)
            {
                Util.addDistinctOption(select,data[i].departmentId,temp+data[i].departmentName);
                position.addOptions(select,data,data[i].departmentId,level+1);
            }
        }
    };
    
    position.showUpdateForm=function(){
        var updateForm=$('formUpdateForPosition');
        var indices = position.datagrid.getSelectedIndices();
        if (indices.length == 0)
	    {
		    MUI.showModalAlert('您必须至少选定一行');
		    return;
	    }
	    if (indices.length > 1)
	    {
		    MUI.showModalAlert('修改时不允许一次修改多行数据。。');
		    return;
	    }
	    var selectedData=position.datagrid.getDataByRow(indices[0]);
        position.addOptions('selectDepartmentId',position.dataForDepartment,0,0);
        //初始化填充本行数据
        Util.setSelectedValue('selectDepartmentId',selectedData.department.departmentId);
        updateForm.getElement('input[name=positionId]').set('value',selectedData.positionId);
        updateForm.getElement('input[name=positionName]').set('value',selectedData.positionName);
        MUI.openContentByWindow('windowUpdateForPosition','修改职位',$('formContainerForPosition'),$('formUpdateForPosition'),400,120);
        MUI.showModalMessage('正在获取权限组列表');
        new Request.JSON({url:'../Ajax/SchoolRoleGroupManage.aspx?Action=search',noCache:true,onSuccess:function(result){
            MUI.hideModalMessage();
            if(result.error)
                MUI.appendStatusMessage({'text':result.msg});
            else
            {
                var data=result.data;
                for(var i=0;i<data.length;i++)
                {
                    Util.addDistinctOption('schoolRoleGroupId2',data[i].roleGroupId,data[i].roleGroupName);
                }
                Util.setSelectedValue('schoolRoleGroupId2',selectedData.schoolRoleGroup.roleGroupId);
            }
        },onFailure:function(error){
	        MUI.appendStatusMessage({'text':'AJAX提交表单的过程中发生未知错误。'});
	        MUI.hideModalMessage();
        }}).get();
    };
    
    position.addFormCheck=function(){
        new FormCheck('formAddForPosition',{display:{tipsPosition:'left'},submit:false,onValidateSuccess:function(){
            MUI.showModalMessage('正在添加职位');
            new Request.JSON({url:'../Ajax/PositionManage.aspx?Action=saveAddPosition',noCache:true,onSuccess:function(result){
                MUI.hideModalMessage();
                if(result.error)
                    MUI.appendStatusMessage({'text':result.msg});
                else
                {
                    $('windowAddForPosition').retrieve('instance').close();
                    MUI.appendStatusMessage({'text':result.msg});
                    position.datagrid.options.page=1;
                    position.datagrid.loadData();
                }
	        },onFailure:function(error){
		        MUI.appendStatusMessage({'text':'AJAX提交表单的过程中发生未知错误。'});
		        MUI.hideModalMessage();
            }}).post($('formAddForPosition'));
        }});   
    };
    
    position.showAddForm=function(){
        var addForm=$('formAddForPosition');
        var departmentId=$('hiddenDepartmentId').get('value');
        if(departmentId=='0')
        {
            MUI.showModalAlert('职位必须添加在某个部门，请先选择一个部门再添加！');
            return;
        }
        addForm.getElement('input[name=departmentId]').set('value',departmentId);
        addForm.getElement('.departmentName').set('text',$('hiddenDepartmentName').get('value'));
        addForm.getElement('input[name=positionName]').set('value','');
        MUI.openContentByWindow('windowAddForPosition','添加职位',$('formContainerForPosition'),$('formAddForPosition'),400,120);
        MUI.showModalMessage('正在获取权限组列表');
        new Request.JSON({url:'../Ajax/SchoolRoleGroupManage.aspx?Action=search',noCache:true,onSuccess:function(result){
            MUI.hideModalMessage();
            if(result.error)
                MUI.appendStatusMessage({'text':result.msg});
            else
            {
                var data=result.data;
                for(var i=0;i<data.length;i++)
                {
                    Util.addDistinctOption('schoolRoleGroupId',data[i].roleGroupId,data[i].roleGroupName);
                }
            }
        },onFailure:function(error){
	        MUI.appendStatusMessage({'text':'AJAX提交表单的过程中发生未知错误。'});
	        MUI.hideModalMessage();
        }}).get();
    };
    
    //获取当前位置并且填充到全局变量position.html必须要有position.getParentDepartmentHtml函数
    position.showLocationHtml=function(departmentId){
        var data=position.dataForDepartment;
        position.html='';
        for(var i=0;i<data.length;i++)
        {
            if(data[i].departmentId==departmentId)
            {
                position.html=data[i].departmentName;
                position.getParentDepartmentHtml(data,data[i].departmentParentId);
            }
        }
    };
    position.getParentDepartmentHtml=function(data,departmentParentId){
        for(var i=0;i<data.length;i++)
        {
            if(data[i].departmentId==departmentParentId)
            {
                position.html=data[i].departmentName+'>>'+position.html;
                position.getParentDepartmentHtml(data,data[i].departmentParentId);
            }
        }
    };
    
    //获取当前位置并且填充到当前位置容器
    position.getParentDepartment=function(data,container,departmentParentId){
        for(var i=0;i<data.length;i++)
        {
            if(data[i].departmentId==departmentParentId)
            {
                container.grab(new Element('span',{text:'>>'}),'top')
                var a=new Element('a',{
                    text:data[i].departmentName,
                    events:{
                        click:function(){
                            var data=this.retrieve('data');
                            position.showLocation(data.departmentId);
                            position.showPositions(data.departmentId);
                        }
                    }
                });
                a.store('data',data[i]);
                container.grab(a,'top');
                position.getParentDepartment(data,container,data[i].departmentParentId);
            }
        }
    };
    
    //加载当前位置
    position.showLocation=function(departmentId){
        var container=$('searchFormForPosition').getElement('.departmentName');
        var data=position.dataForDepartment;
        container.empty();
        for(var i=0;i<data.length;i++)
        {
            if(data[i].departmentId==departmentId)
            {
                container.grab(new Element('span',{text:data[i].departmentName}));
                position.getParentDepartment(data,container,data[i].departmentParentId);
            }
        }
    };
    
    //加载当前部门的所有职位
    position.showPositions=function(departmentId){
        position.datagrid.options.url='../Ajax/PositionManage.aspx?Action=search&departmentId='+departmentId.toString()
        position.datagrid.options.page=1;
        position.datagrid.loadData();
    };
    
    //创建属性目录
    position.buildTreeList=function(data){
        var treeContainer=$('leftForPosition');
        treeContainer.empty();
        var ul=new Element('ul',{id:'treeForPosition','class': 'tree'});
        treeContainer.grab(ul);
        position.toTreeHtml(ul,data,-1);
    };
    
    //创建树形目录所需要的递归函数
    position.toTreeHtml=function(parentContainer,data,departmentParentId){
        for(var i=0;i<data.length;i++)
        {
            if(data[i].departmentParentId==departmentParentId)
            {
                var isChild=false;
                for(j=0;j<data.length;j++)
                {
                    if(data[j].departmentParentId==data[i].departmentId)
                    {
                        isChild=true;
                        break;
                    }
                }
                var li=new Element('li',{'class':'folder'});
                var span=new Element('span');
                var a=new Element('a',{
                    text:data[i].departmentName,
                    events:{
                        click:function(){
                            var data=this.retrieve('data');
                            position.showLocation(data.departmentId);
                            position.showPositions(data.departmentId);
                            $('hiddenDepartmentId').set('value',data.departmentId);
                            $('hiddenDepartmentName').set('value',data.departmentName);
                        }
                    }
                });
                a.store('data',data[i]);
                parentContainer.grab(li);
                li.grab(span);
                span.grab(a);
                if(isChild)
                {
                    li.addClass('f-open');
                    var ul=new Element('ul');
                    li.grab(ul);
                    position.toTreeHtml(ul,data,data[i].departmentId);
                }
            }
        }
    };
    
    //布局整个页面
    position.layout=function(){
        new MUI.Column({
            container: $('leftForPosition').getParent().getParent(),
		    id: 'leftColumnForPosition',
		    placement: 'left',
		    width: 200,
		    resizeLimit: [100, 300]
	    });
	    new MUI.Column({
	        container: $('leftForPosition').getParent().getParent(),
		    id: 'rightColumnForPosition',
		    placement: 'main'
	    });
        new MUI.Panel({
            header: false,
		    id: 'leftPanelForPosition',		
		    content: $('leftForPosition'),
		    column: 'leftColumnForPosition'
	    });
	    new MUI.Panel({
	        header: false,
		    id: 'rightPanelForPosition',
		    content: $('rightForPosition'),
		    column: 'rightColumnForPosition',
		    padding: { top: 0, right: 0, bottom: 0, left: 0 }
	    });
    };
    
    
    position.loadData=function(){
        MUI.showModalMessage('正在加载部门数据');
        new Request.JSON({url:'../Ajax/DepartmentManage.aspx?Action=search',noCache:true,onSuccess:function(result){
            MUI.hideModalMessage();
            if(result.error)
                MUI.appendStatusMessage({'text':result.msg});
            else
            {
                position.dataForDepartment=result.data;
                position.buildTreeList(result.data);
                if (buildTree) buildTree('treeForPosition');
            }
		},onFailure:function(error){
		    MUI.appendStatusMessage({'text':'AJAX提交表单的过程中发生未知错误。'});
			MUI.hideModalMessage();
	    }}).get();
	    position.datagrid = new omniGrid('rightForPosition', {
            columnModel: [
                {header: '职位ID',dataIndex: 'positionId',dataType:'number',width:80,sortable:true},
                {header: '职位名称',dataIndex: 'positionName',dataType:'string',width:100,sortable:false},
                {header: '关联权限组',dataIndex: 'positionId',dataType:'string',width:100,sortable:false,labelFunction:function(rowdata, r, columnModel){
                    return $defined(rowdata.schoolRoleGroup)?rowdata.schoolRoleGroup.roleGroupName:'&nbsp;';
                }},
                {header: '所属部门位置',dataIndex: 'positionId',dataType:'string',width:200,sortable:false,labelFunction:function(rowdata, r, columnModel){
                    if($defined(rowdata.department))
                    {
                        position.showLocationHtml(rowdata.department.departmentId);
                        return position.html;
                    }
                    else
                        return '&nbsp;';
                }},
                {header: '该职位教师',dataIndex: 'positionId',dataType:'string',width:300,sortable:false,labelFunction:function(rowdata, r, columnModel){
                    if($defined(rowdata.teachers))
                    {
                        var temp='';
                        if(i<rowdata.teachers.length>0)
                        {
                            for(var i=0;i<rowdata.teachers.length;i++)
                            {
                                temp+=rowdata.teachers[i].teacherName+',';
                            }
                        }
                        else
                        temp='&nbsp;';
                    }
                    else
                        temp='&nbsp;';
                    return temp;
                }}
            ],
            buttons : [
                {name: '添加', bclass: 'add', onclick : position.showAddForm},
                {name: '修改', bclass: 'update', onclick : position.showUpdateForm},
                {name: '删除', bclass: 'delete', onclick : position.deleteForPosition}
            ],
            url:'../Ajax/PositionManage.aspx?Action=search&departmentId=0',
            urlMethod:'post',
            urlPostForm:'searchFormForPosition',
            perPageOptions: [20,50,100,200,500,1000],
            perPage:20,
            height:$('rightForPosition').getParent().getParent().getStyle('height').toInt(),
            serverSort:false,
            multipleSelection:false,
            sortHeader:true,
            pagination:true
        });
    };
    
    position.binddingEvent=function(){
    };
    
    position.initialize=function(){
        new ZebraTable({elements:'table.tableFixed'});
        position.layout();
        position.loadData();
        position.binddingEvent();
        position.addFormCheck();
        position.updateFormCheck();
    };
    
    position.init=function(){
        position.initialize();
    };
    
    position.init();
});
</script>
<div id="leftForPosition"></div>
<div id="rightForPosition"></div>

<!--隐藏的表单-->
<div id="formContainerForPosition" style="display:none;">
<!--搜索条件表单-->
<form id="searchFormForPosition">
    <input type="hidden" id="hiddenDepartmentId" name="hiddenDepartmentId" value="0" />
    <input type="hidden" id="hiddenDepartmentName" name="hiddenDepartmentName" value="<%=school.schoolName %>" />
    <span class="span">当前部门：</span>
    <span class="departmentName"><%=school.schoolName %></span>
</form>
<!--添加表单-->
<form id="formAddForPosition">
<input type="hidden" name="departmentId" />
<table class="tableFixed" border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr>
        <td class="tableLeft1">所属部门：</td>
        <td class="tableLeft2"><span class="departmentName"></span></td>
    </tr>
    <tr>
        <td class="tableLeft1">职位名称：</td>
        <td class="tableLeft2"><input class="input validate['required','length[2,10]']" name="positionName" type="text" /></td>
    </tr>
    <tr>
        <td class="tableLeft1">关联权限组：</td>
        <td class="tableLeft2"><select id="schoolRoleGroupId" name="schoolRoleGroupId" class="input"><option value="0">请选择权限组</option></select></td>
    </tr>
    <tr>
        <td class="tableLeft1"></td>
        <td class="tableLeft2"><input class="submit button validate['submit']" type="button" value="提交添加" /></td>
    </tr>
</table>
</form>
<!--修改表单-->
<form id="formUpdateForPosition">
<input type="hidden" name="positionId" />
<table class="tableFixed" border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr>
        <td class="tableLeft1">所属部门：</td>
        <td class="tableLeft2"><select id="selectDepartmentId" class="input" name="departmentId"></select></td>
    </tr>
    <tr>
        <td class="tableLeft1">职位名称：</td>
        <td class="tableLeft2"><input class="input validate['required','length[2,10]']" name="positionName" type="text" /></td>
    </tr>
    <tr>
        <td class="tableLeft1">关联权限组：</td>
        <td class="tableLeft2"><select id="schoolRoleGroupId2" name="schoolRoleGroupId" class="input"><option value="0">请选择权限组</option></select></td>
    </tr>
    <tr>
        <td class="tableLeft1"></td>
        <td class="tableLeft2"><input class="submit button validate['submit']" type="button" value="提交添加" /></td>
    </tr>
</table>
</form>
</div>
